<html>
<head><meta charset="utf-8"><title>__tls_get_addr and TLS in dylibs · t-compiler/performance · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/index.html">t-compiler/performance</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html">__tls_get_addr and TLS in dylibs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213971232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213971232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213971232">(Oct 20 2020 at 19:54)</a>:</h4>
<p>heh the top 6 functions (and their counts) in the 13GB <code>ltrace -l '*'</code> output are:</p>
<div class="codehilite"><pre><span></span><code>25041736 __tls_get_addr
   1259 pthread_once
   1074 malloc
    483 pthread_rwlock_wrlock
    483 pthread_rwlock_unlock
     11 calloc
</code></pre></div>



<a name="213971311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213971311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213971311">(Oct 20 2020 at 19:54)</a>:</h4>
<p>so I think the reason it took so long and produced so much output is it traced <code>__tls_get_addr</code></p>



<a name="213972350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972350">(Oct 20 2020 at 20:03)</a>:</h4>
<p>Hmm, didn't <span class="user-mention" data-user-id="133169">@matklad</span> have a post about how <code>thread_local!</code>'s API required it to be slow? I wonder if we could improve that in rustc somehow</p>



<a name="213972370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972370">(Oct 20 2020 at 20:03)</a>:</h4>
<p>the problem is dylibs are bad</p>



<a name="213972486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972486">(Oct 20 2020 at 20:04)</a>:</h4>
<p>Dylibs force a slower implementation of tls</p>



<a name="213972498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972498">(Oct 20 2020 at 20:04)</a>:</h4>
<p>AFAIK <code>thread_local!</code> just uses <code>#[thread_local]</code> under the hood, which presumably LLVM generates <code>__tls_get_addr</code> calls for</p>



<a name="213972570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972570">(Oct 20 2020 at 20:05)</a>:</h4>
<p>For executables it is possible to directly offset from a pointer stored at %fs, but for dylibs the offset is not known, so you have to call __tls_get_addr.</p>



<a name="213972577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972577">(Oct 20 2020 at 20:05)</a>:</h4>
<p>we don't need dylibs for <code>rustc</code>, we just need "shared objects" for deduplication. but idk if existing toolchains even allow statically linking <code>.so</code>s</p>



<a name="213972715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972715">(Oct 20 2020 at 20:06)</a>:</h4>
<p>How would that work with loading compiler plugins/codegen backends that link against rustc_driver?</p>



<a name="213972847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972847">(Oct 20 2020 at 20:07)</a>:</h4>
<p>they would be dynamically loaded libraries, but if they accessed some TLS defined in <code>librustc_driver.so</code> they'd be accessing that via a statically computed offset</p>



<a name="213972957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213972957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213972957">(Oct 20 2020 at 20:08)</a>:</h4>
<p>though I wonder if you can link both libc and such a weird <code>.so</code>. anyway, it's theoretically possible, practically it's probably unlikely</p>



<a name="213973021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973021">(Oct 20 2020 at 20:09)</a>:</h4>
<p>and I suppose you couldn't have two... waaaait a minute</p>



<a name="213973044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973044">(Oct 20 2020 at 20:09)</a>:</h4>
<p>That could only work if you can guarantee that a single such dylib is linked into a process</p>



<a name="213973046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973046">(Oct 20 2020 at 20:09)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> why isn't this all done with relocations, like GOT &amp; friends?</p>



<a name="213973072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973072">(Oct 20 2020 at 20:09)</a>:</h4>
<p>why does it have to involve a function call</p>



<a name="213973196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973196">(Oct 20 2020 at 20:10)</a>:</h4>
<p>The TLS storage for dylibs is lazily created for each thread when necessary I believe.</p>



<a name="213973220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973220">(Oct 20 2020 at 20:10)</a>:</h4>
<p>The offset can thus differ between threads in the same process.</p>



<a name="213973288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973288">(Oct 20 2020 at 20:11)</a>:</h4>
<p>see, that we definitely don't need :(</p>



<a name="213973395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973395">(Oct 20 2020 at 20:12)</a>:</h4>
<p>There isn't much of a choice as threads may already exist and multiple threads may load a new dylib at the same time.</p>



<a name="213973436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973436">(Oct 20 2020 at 20:12)</a>:</h4>
<p>right, I don't want <code>librustc_driver.so</code> to be dynamically loadable</p>



<a name="213973519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973519">(Oct 20 2020 at 20:13)</a>:</h4>
<p>I wonder if any of these are relevant <a href="https://llvm.org/docs/LangRef.html#thread-local-storage-models">https://llvm.org/docs/LangRef.html#thread-local-storage-models</a></p>



<a name="213973549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973549">(Oct 20 2020 at 20:13)</a>:</h4>
<p>It should technically be possible to roll our own tls implementation specifically for rustc, though the question would then be where to store the tls pointer when %fs is already uses.</p>



<a name="213973666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973666">(Oct 20 2020 at 20:14)</a>:</h4>
<p>I mean we can just statically allocate the right amount of space in every executable that links to <code>librustc_driver.so</code></p>



<a name="213973678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973678">(Oct 20 2020 at 20:14)</a>:</h4>
<p>and then put the offset in a weak symbol or something</p>



<a name="213973726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973726">(Oct 20 2020 at 20:15)</a>:</h4>
<p>Yes those storage models are relevant. The linker tries to change the tls storage model to the fastest possible way. In fact it required the tls access to have a specific sequence of instructions including padding prefixes to be able to rewrite it for the chosen storage model.</p>



<a name="213973869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973869">(Oct 20 2020 at 20:16)</a>:</h4>
<p>If we depend on the libc tls implementation then the offset can differ between threads. If we don't, where do we store the thread specific pointer?</p>



<a name="213973900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973900">(Oct 20 2020 at 20:16)</a>:</h4>
<p>anyway this doesn't matter much, I just need to avoid tracing <code>__tls_get_addr</code> if I use <code>ltrace</code> again</p>



<a name="213973998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213973998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213973998">(Oct 20 2020 at 20:17)</a>:</h4>
<p>Every query accesses the TLS I think, so improvements to the TLS implementation can have a relatively big effect.</p>



<a name="213974001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974001">(Oct 20 2020 at 20:17)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> no, I mean we rely on the executable TLS which has the same offset across all threads</p>



<a name="213974112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974112">(Oct 20 2020 at 20:18)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/247081-t-compiler.2Fperformance/topic/hardware.20counter.20(rdpmc).20measurements/near/213973998">said</a>:</p>
<blockquote>
<p>Every query accesses the TLS I think, so improvements to the TLS implementation can have a relatively big effect.</p>
</blockquote>
<p>I'll open an issue so we don't forget</p>



<a name="213974118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974118">(Oct 20 2020 at 20:18)</a>:</h4>
<p>and then we inject that into the dylib somehow. could even be something we do at runtime</p>



<a name="213974171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974171">(Oct 20 2020 at 20:18)</a>:</h4>
<p>roll our own <code>ld.so</code>, how hard could it be...</p>



<a name="213974226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974226">(Oct 20 2020 at 20:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> <a href="#narrow/stream/247081-t-compiler.2Fperformance/topic/hardware.20counter.20(rdpmc).20measurements/near/213974118">said</a>:</p>
<blockquote>
<p>and then we inject that into the dylib somehow. could even be something we do at runtime</p>
</blockquote>
<p>That could work. <code>__rustc_get_tls_addr()</code>?</p>



<a name="213974245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974245">(Oct 20 2020 at 20:19)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> we don't have the go that far I don't think, just add in an unstable hack that relies on the faster "executable TLS" support that already is</p>



<a name="213974250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974250">(Oct 20 2020 at 20:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/247081-t-compiler.2Fperformance/topic/hardware.20counter.20(rdpmc).20measurements/near/213974171">said</a>:</p>
<blockquote>
<p>roll our own <code>ld.so</code>, how hard could it be...</p>
</blockquote>
<p>relibc?</p>



<a name="213974251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974251">(Oct 20 2020 at 20:19)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> heh</p>



<a name="213974349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974349">(Oct 20 2020 at 20:20)</a>:</h4>
<p>And a macro for every rustc_driver user to define it.</p>



<a name="213974354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974354">(Oct 20 2020 at 20:20)</a>:</h4>
<p>anyway this is kind of offtopic, I'm just sad that's what I wasted time on</p>



<a name="213974371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974371">(Oct 20 2020 at 20:20)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> lol in that case we wouldn't even need to implement it in the compiler itself</p>



<a name="213974412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974412">(Oct 20 2020 at 20:21)</a>:</h4>
<p>Split this into a new topic?</p>



<a name="213974417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974417">(Oct 20 2020 at 20:21)</a>:</h4>
<p>like we wouldn't need <code>#[more_magical_thread_local]</code></p>



<a name="213974448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974448">(Oct 20 2020 at 20:21)</a>:</h4>
<p>hmm yeah. I guess it's still performance-related, so that's fine. gimme a sec</p>



<a name="213974734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974734">(Oct 20 2020 at 20:23)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> mentioned elsewhere that <code>ld.so</code> might have a more advanced TLS dynamic linking, but everything still uses <code>__tls_get_addr()</code></p>



<a name="213974797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974797">(Oct 20 2020 at 20:24)</a>:</h4>
<p>so maybe we just need to find a few toggles to mess with in order to get faster TLS</p>



<a name="213974823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974823">(Oct 20 2020 at 20:24)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120989">@njn</span> <span class="user-mention" data-user-id="133169">@matklad</span></p>



<a name="213974866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213974866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213974866">(Oct 20 2020 at 20:24)</a>:</h4>
<p>but yeah worst case we can bodge it <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="213975451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213975451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213975451">(Oct 20 2020 at 20:30)</a>:</h4>
<p>The initialexec storage model seems to be allowed for dylibs that are loaded at startup. That only requires 2 instructions. Neither of which is a call.</p>



<a name="213975984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213975984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213975984">(Oct 20 2020 at 20:35)</a>:</h4>
<p>We have <code>-Z tls-model=initial-exec</code>, so someone is welcome to try it on the compiler crates.</p>



<a name="213977184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213977184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213977184">(Oct 20 2020 at 20:46)</a>:</h4>
<p>add that somewhere in <code>src/bootstrap</code>, run perf, ???, profit?</p>



<a name="213977606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213977606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213977606">(Oct 20 2020 at 20:49)</a>:</h4>
<p>I guess so. It should probably only be added for <a href="http://librustc_driver.so">librustc_driver.so</a> though, so it should go in <a href="http://build.rs">build.rs</a>. Not everyone may play well with initialexec as tls storage model.</p>



<a name="213977700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213977700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213977700">(Oct 20 2020 at 20:50)</a>:</h4>
<p>ah right. there's also <code>libstd.so</code> but I don't know how bad that one is</p>



<a name="213977864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213977864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213977864">(Oct 20 2020 at 20:51)</a>:</h4>
<p>That one may be indirectly loaded using dlopen when for example writing a plugin for something and using -Cprefer-dynamic.</p>



<a name="213977966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213977966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213977966">(Oct 20 2020 at 20:52)</a>:</h4>
<p>wouldn't it always be loaded at the start? or do you mean a second copy of <code>libstd-*.so</code>?</p>



<a name="213978156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213978156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213978156">(Oct 20 2020 at 20:54)</a>:</h4>
<p>I mean as plugin for a non-rust program.</p>



<a name="213978424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213978424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213978424">(Oct 20 2020 at 20:57)</a>:</h4>
<blockquote>
<p>AFAIK thread_local! just uses #[thread_local] under the hood, which presumably LLVM generates __tls_get_addr calls for</p>
</blockquote>
<p>It also has the RefCell-ish thing for tracking "is this initialized", and can prevent compiler from hoisting thread local access out of loops in pathalogical cases. But, given rustc's already verbose TLS API with with, I wouldn't expect that to matter much</p>



<a name="213979632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213979632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213979632">(Oct 20 2020 at 21:08)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> ah because we don't have a private copy of <code>libstd-*.so</code> for <code>rustc</code>, I see</p>



<a name="213979705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213979705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213979705">(Oct 20 2020 at 21:09)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> tbh we could just use <code>#[thread_local]</code> directly in the compiler if we wanted to, there's nothing stopping us I don't think</p>



<a name="213979745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213979745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213979745">(Oct 20 2020 at 21:09)</a>:</h4>
<p>whereas the dylib tax is worse to shave off</p>



<a name="213985325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213985325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213985325">(Oct 20 2020 at 22:03)</a>:</h4>
<p>On windows, I don't believe a function call is ever necessary to access TLS, and there no restrictions on when a shared library is loaded. This is because memory for TLS slots is allocated eagerly using TlsAlloc whenever a module is loaded. I don't know if such a thing is in practice possible on linux, but in theory it should be...</p>



<a name="213995631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/213995631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#213995631">(Oct 21 2020 at 00:26)</a>:</h4>
<p>It's not just dylibs that incur the cost, it depends how they're loaded (e.g. via dlopen vs not), and whether code outside the module needs to access them. right now rustc always compiles it in the most pessimistic/general possible way.</p>



<a name="214011246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214011246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214011246">(Oct 21 2020 at 06:09)</a>:</h4>
<p>I would love to see rustc (and Rust code in general) able to use the much simpler "just use <code>%fs</code>" approach.</p>



<a name="214011253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214011253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214011253">(Oct 21 2020 at 06:09)</a>:</h4>
<p>I suspect that'd make a <em>massive</em> performance difference, given the number of calls to <code>__tls_get_addr</code>.</p>



<a name="214011259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214011259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214011259">(Oct 21 2020 at 06:09)</a>:</h4>
<p>I remember profiling some rayon code and finding that <code>__tls_get_addr</code> and family was a <em>substantial</em> component of runtime.</p>



<a name="214013006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214013006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214013006">(Oct 21 2020 at 06:44)</a>:</h4>
<p>Also, I would love to avoid the additional level of indirection through a hash table, that the common thread-local libraries in the ecosystem introduce.</p>



<a name="214042813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214042813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214042813">(Oct 21 2020 at 12:31)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> AFAIK <code>rustc</code> is much worse than the average Rust program just because the entirety of it is in a dylib</p>



<a name="214043402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214043402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214043402">(Oct 21 2020 at 12:38)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=598ebc2ff99a54185588bd891e7dc064">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=598ebc2ff99a54185588bd891e7dc064</a></p>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code>    <span class="nf">mov</span> <span class="no">rax</span><span class="p">,</span> <span class="no">qword</span> <span class="no">ptr</span> <span class="no">fs</span><span class="p">:[</span><span class="mi">0</span><span class="p">]</span>
    <span class="nf">lea</span> <span class="no">rdi</span><span class="p">,</span> <span class="p">[</span><span class="no">rax</span> <span class="err">+</span> <span class="no">playground</span><span class="p">::</span><span class="no">PID</span><span class="p">::</span><span class="no">__getit</span><span class="p">::</span><span class="no">__KEY@TPOFF</span><span class="p">]</span>
</code></pre></div>



<a name="214043471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214043471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214043471">(Oct 21 2020 at 12:38)</a>:</h4>
<p>I guess this is pre-linking, hence <code>@TPOFF</code></p>



<a name="214052817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214052817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214052817">(Oct 21 2020 at 13:56)</a>:</h4>
<p>how did you get such clean disassembly out of the playground?</p>



<a name="214053122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214053122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214053122">(Oct 21 2020 at 13:58)</a>:</h4>
<p>oh, nice, there's an option for intel syntax, I had no idea that existed</p>



<a name="214053185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214053185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214053185">(Oct 21 2020 at 13:59)</a>:</h4>
<p>why is a dylib a quality problem?</p>



<a name="214054741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214054741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214054741">(Oct 21 2020 at 14:10)</a>:</h4>
<p>There are multiple storage models for TLS. The local exec model, which is the fastest is only possible for executables. The initial exec model is a tiny bit slower. This one also supports dylibs, but only when they are loaded at program startup, not when using <code>dlopen</code>. The general dynamic model is always allowed, but it requires a <code>__tls_get_addr</code> call. Because it is not known if dylibs will be <code>dlopen</code>ed in advance, they use the general dynamic model by default. Executables instead use the local exec model, which is the fastest model.</p>



<a name="214054971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214054971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214054971">(Oct 21 2020 at 14:11)</a>:</h4>
<p>See <a href="https://akkadia.org/drepper/tls.pdf">https://akkadia.org/drepper/tls.pdf</a> for all possible models and their resulting codegen.</p>



<a name="214055656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214055656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214055656">(Oct 21 2020 at 14:17)</a>:</h4>
<p>so it's not controversial</p>



<a name="214055696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214055696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oliver <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214055696">(Oct 21 2020 at 14:17)</a>:</h4>
<p>I've wanted to learn more about ELF</p>



<a name="214056199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214056199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214056199">(Oct 21 2020 at 14:21)</a>:</h4>
<p>All thread locals in the same module are combined by the linker into a single contiguous block "template", that is instantiated for each thread in the process (resulting in up to <code>num_threads * num_modules</code> blocks).</p>
<p>Each thread has an index, whose address is stored in <code>fs</code>, such that you can take your module ID look it up in that index, and find the address of the block for that thread/module combination.</p>
<p>The problem happens when the block has not been allocated yet, and is not present in the index. In that case it must be lazily allocated, and this logic is complex enough that it lives in a function <code>__tls_get_addr</code> - it doesn't make sense to inline it into the caller.</p>
<p>For module 0 (the executable) and other modules loaded at startup I believe space in the index can be reserved ahead of time, and so when accessing a TLS value, it can be inlined into the caller (and is just a couple of de-references). For dylibs, they cannot rely on space being pre-allocated, and so will always take the slow path.</p>
<p>If you switch to the <code>inital exec</code> model, then dylibs will assume that they are loaded on startup. However, AIUI this will cause corruption if those dylibs are loaded with <code>dlopen</code>.</p>
<p>For comparison, on windows, the OS provides hooks for all DLLs loaded by a process to detect when a new thread is started. This allows all TLS space to be reserved eagerly, even if a module is loaded dynamically with <code>LoadLibrary</code>. All code running in anywhere is safe to assume that TLS space has been allocated, and the only overhead to TLS access from a DLL is the usual overhead of relocations.</p>



<a name="214061640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214061640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214061640">(Oct 21 2020 at 14:57)</a>:</h4>
<p>why can't they just error from <code>dlopen</code> &gt;:(</p>



<a name="214061670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214061670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214061670">(Oct 21 2020 at 14:57)</a>:</h4>
<p>(if they weren't built to be <code>dlopen</code>-able)</p>



<a name="214062239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214062239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Diggsey <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214062239">(Oct 21 2020 at 15:01)</a>:</h4>
<p>I guess because they <em>might</em> work if they happened to be allocated already? TBH I don't know - it may be that this mitigation is implemented in some cases, but the first result on google (<a href="https://lists.freebsd.org/pipermail/freebsd-arch/2016-February/017699.html">https://lists.freebsd.org/pipermail/freebsd-arch/2016-February/017699.html</a>) indicated that this was not the case</p>



<a name="214071606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214071606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214071606">(Oct 21 2020 at 15:57)</a>:</h4>
<blockquote>
<p>Beside the program header entry the only other information the dynamic linker<br>
needs is the DF STATIC TLS flag in the DT FLAGS entry in the dynamic section. This<br>
flag allows to reject loading modules dynamically which are created with the static model</p>
</blockquote>



<a name="214073029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214073029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214073029">(Oct 21 2020 at 16:05)</a>:</h4>
<p>that would be sweet to have for <code>librustc_driver.so</code></p>



<a name="214073752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214073752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214073752">(Oct 21 2020 at 16:09)</a>:</h4>
<p>Why <em>do</em> we put that in a shared library, anyway? What shares it?</p>



<a name="214074181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074181">(Oct 21 2020 at 16:12)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> all the <code>rustc</code> drivers like <code>rustdoc</code>, <code>clippy</code>, <code>miri</code>, etc.</p>



<a name="214074234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074234">(Oct 21 2020 at 16:12)</a>:</h4>
<p>Ah, got it.</p>



<a name="214074399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074399">(Oct 21 2020 at 16:13)</a>:</h4>
<p>They aren't using dlopen, right? So in theory we don't need tls_get_addr?</p>



<a name="214074431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074431">(Oct 21 2020 at 16:13)</a>:</h4>
<p>yes, we just have to figure out how to make LLVM/the linker do what we want</p>



<a name="214074541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074541">(Oct 21 2020 at 16:14)</a>:</h4>
<p>maybe it's just the <code>-Z tls-model=initial-exec</code> flag <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> mentioned but I haven't tried it</p>



<a name="214074556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214074556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214074556">(Oct 21 2020 at 16:14)</a>:</h4>
<p>if someone has the time, feel free to play around with it <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="214097795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214097795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214097795">(Oct 21 2020 at 18:57)</a>:</h4>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> How did you manage to reliably ltrace rustc? I got non-deterministic errors like <code>rustc: ../nptl/pthread_mutex_lock.c:81: __pthread_mutex_cond_lock: Assertion `mutex-&gt;__data.__owner == 0' failed.</code> when I tried.</p>



<a name="214100396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214100396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214100396">(Oct 21 2020 at 19:15)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> what's the full command?</p>



<a name="214100583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214100583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214100583">(Oct 21 2020 at 19:16)</a>:</h4>
<p>I never got any assertion failures no matter what I did</p>



<a name="214100668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214100668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214100668">(Oct 21 2020 at 19:17)</a>:</h4>
<p><code>-l '*'</code> is the filter I used to get the gazillion <code>__tls_get_addr</code> calls</p>



<a name="214101279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214101279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214101279">(Oct 21 2020 at 19:21)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> oof tried to replicate something and forgot to pipe stderr instead of stdout</p>



<a name="214101322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214101322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214101322">(Oct 21 2020 at 19:21)</a>:</h4>
<p>oh thank god that <code>less</code> switched to alternate buffer so it didn't pollute my terminal <span aria-label="rofl" class="emoji emoji-1f923" role="img" title="rofl">:rofl:</span></p>



<a name="214101930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214101930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214101930">(Oct 21 2020 at 19:26)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> this works great if you want to see <code>__tls_get_addr</code> calls: <code>ltrace -e __tls_get_addr -f -w10</code></p>



<a name="214102002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102002">(Oct 21 2020 at 19:26)</a>:</h4>
<p>replacing the <code>-e __tls_get_addr</code> filter with <code>-l '*'</code> also works but will include other calls. <code>-w10</code> gives you 10 frames of stack trace, you can tune that</p>



<a name="214102046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102046">(Oct 21 2020 at 19:27)</a>:</h4>
<p><code>ltrace -f -e __tls_get_addr -o /tmp/ltrace rustc src/main.rs</code></p>



<a name="214102052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102052">(Oct 21 2020 at 19:27)</a>:</h4>
<p>using <code>rustfilt | c++filt</code> on the result seems to clean it up nicely</p>



<a name="214102083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102083">(Oct 21 2020 at 19:27)</a>:</h4>
<p>hmm</p>



<a name="214102098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102098">(Oct 21 2020 at 19:27)</a>:</h4>
<p>Just running that gives me the assertion failure sometimes. And when I added things like <code>-T</code>, I also sometimes got segfaults from ltrace.</p>



<a name="214102132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102132">(Oct 21 2020 at 19:28)</a>:</h4>
<p>("sometimes", one of the most evil words in software development.)</p>



<a name="214102333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102333">(Oct 21 2020 at 19:29)</a>:</h4>
<p><code>echo 'fn main() {}' | ltrace -e __tls_get_addr -f -w5 ./build/x86_64-unknown-linux-gnu/stage1/bin/rustc - 2&gt;| rustfilt | c++filt | head -n 5000 | tail -n 30</code></p>



<a name="214102382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102382">(Oct 21 2020 at 19:29)</a>:</h4>
<p>so this works fine. pretty slow and it's still inside LLVM that early, hmm</p>



<a name="214102415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102415">(Oct 21 2020 at 19:29)</a>:</h4>
<p>No idea.</p>



<a name="214102448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102448">(Oct 21 2020 at 19:30)</a>:</h4>
<p>now trying with just <code>rustc</code> so it goes through the <code>rustup</code> wrapper.... and it still works</p>



<a name="214102545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102545">(Oct 21 2020 at 19:30)</a>:</h4>
<p>oh cool the nightly built has a dynamically linked <code>libLLVM.so</code>, so I can filter by that</p>



<a name="214102697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102697">(Oct 21 2020 at 19:31)</a>:</h4>
<p><code>echo 'fn main() {}' | ltrace -e '__tls_get_addr-@*LLVM*' -f -w5 rustc - 2&gt;| rustfilt | c++filt | head -n 1000 | tail</code></p>



<a name="214102755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102755">(Oct 21 2020 at 19:31)</a>:</h4>
<p>this works great, it's showing me some deserialization going to <code>Symbol::intern</code></p>



<a name="214102840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102840">(Oct 21 2020 at 19:32)</a>:</h4>
<p>(I'm using <code>head -n ... | tail</code> to limit the output and not have to wait ages, it's incredibly slow to trace <code>__tls_get_addr</code>)</p>



<a name="214102869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102869">(Oct 21 2020 at 19:32)</a>:</h4>
<p>Hmmm. In <code>src/bootstrap/bin/rustc.rs</code>, how can I distinguish "crates that won't get shipped in the sysroot"?</p>



<a name="214102911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102911">(Oct 21 2020 at 19:32)</a>:</h4>
<p>you probably just want to hardcode based on the name being <code>rustc_driver</code></p>



<a name="214102924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102924">(Oct 21 2020 at 19:32)</a>:</h4>
<p>if I had to guess</p>



<a name="214102945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102945">(Oct 21 2020 at 19:33)</a>:</h4>
<p>I'd like to cover the rest of the <code>rustc_*</code> crates too.</p>



<a name="214102957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102957">(Oct 21 2020 at 19:33)</a>:</h4>
<p>And anything they depend on.</p>



<a name="214102986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102986">(Oct 21 2020 at 19:33)</a>:</h4>
<p>but they're <code>rlib</code>s, wouldn't they be "polymorphic"?</p>



<a name="214102995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214102995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214102995">(Oct 21 2020 at 19:33)</a>:</h4>
<p>actually I wonder how that works hmm</p>



<a name="214103005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103005">(Oct 21 2020 at 19:33)</a>:</h4>
<p>I don't <em>think</em> that works.</p>



<a name="214103026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103026">(Oct 21 2020 at 19:33)</a>:</h4>
<p>I think that gets decided when compiling the crate, unfortunately.</p>



<a name="214103039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103039">(Oct 21 2020 at 19:33)</a>:</h4>
<p>but surely if you have a <code>thread_local!</code> in a crate and statically link to it, it won't call <code>__tls_get_addr</code>?</p>



<a name="214103041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103041">(Oct 21 2020 at 19:34)</a>:</h4>
<p>It <em>should</em> be determined by the top-level compile.</p>



<a name="214103103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103103">(Oct 21 2020 at 19:34)</a>:</h4>
<p>I guess we should determine this first</p>



<a name="214103113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103113">(Oct 21 2020 at 19:34)</a>:</h4>
<p>Yeah.</p>



<a name="214103146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103146">(Oct 21 2020 at 19:34)</a>:</h4>
<div class="codehilite" data-code-language="Bash Session"><pre><span></span><code><span class="gp">$</span> <span class="nb">echo</span> <span class="s1">'fn main() {}'</span> <span class="p">|</span> ltrace -e <span class="s1">'__tls_get_addr-@*LLVM*'</span> -f rustc - <span class="m">2</span>&gt;<span class="p">|</span> wc -l
<span class="go">89723</span>
</code></pre></div>



<a name="214103179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103179">(Oct 21 2020 at 19:35)</a>:</h4>
<p>so that's nowhere near as slow as compiling <code>libcore</code> :P</p>



<a name="214103242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103242">(Oct 21 2020 at 19:35)</a>:</h4>
<div class="codehilite" data-code-language="Bash Session"><pre><span></span><code><span class="gp">$</span> <span class="nb">echo</span> <span class="s1">''</span> <span class="p">|</span> ltrace -e <span class="s1">'__tls_get_addr-@*LLVM*'</span> -f rustc --crate-type<span class="o">=</span>lib --emit<span class="o">=</span>metadata - <span class="m">2</span>&gt;<span class="p">|</span> wc -l
<span class="go">48705</span>
</code></pre></div>



<a name="214103257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103257">(Oct 21 2020 at 19:35)</a>:</h4>
<p>and this only takes a few seconds</p>



<a name="214103273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103273">(Oct 21 2020 at 19:35)</a>:</h4>
<p>Hang on.</p>



<a name="214103291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103291">(Oct 21 2020 at 19:36)</a>:</h4>
<p>That's the crate type you're compiling <em>to</em>, not the crate type of rustc. :)</p>



<a name="214103356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103356">(Oct 21 2020 at 19:36)</a>:</h4>
<p>no I'm testing <code>ltrace</code></p>



<a name="214103362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103362">(Oct 21 2020 at 19:36)</a>:</h4>
<p>Ah.</p>



<a name="214103393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103393">(Oct 21 2020 at 19:36)</a>:</h4>
<p>Interestingly, you're getting a lot more TLS calls than I am.</p>



<a name="214103402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103402">(Oct 21 2020 at 19:36)</a>:</h4>
<p>so using this command I can confirm that it finishes successfully: <code>echo '' | ltrace -e '__tls_get_addr-@*LLVM*' -f rustc --crate-type=lib --emit=metadata - 2&gt;| tail -n 1000 | less</code></p>



<a name="214103433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103433">(Oct 21 2020 at 19:36)</a>:</h4>
<p>oops <code>rustc 1.46.0-nightly (8aa18cbdc 2020-07-08)</code></p>



<a name="214103436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103436">(Oct 21 2020 at 19:37)</a>:</h4>
<p>stuck on old nightly</p>



<a name="214103492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103492">(Oct 21 2020 at 19:37)</a>:</h4>
<div class="codehilite"><pre><span></span><code>$ ltrace -f -e __tls_get_addr rustc src/main.rs 2&gt;&amp;1 | wc -l
62906
</code></pre></div>



<a name="214103510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103510">(Oct 21 2020 at 19:37)</a>:</h4>
<p>(where main is the stock hello world that <code>cargo new</code> creates.)</p>



<a name="214103534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103534">(Oct 21 2020 at 19:37)</a>:</h4>
<p>(I prefer not relying on files because then it's easy to weed out differences)</p>



<a name="214103637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103637">(Oct 21 2020 at 19:38)</a>:</h4>
<div class="codehilite"><pre><span></span><code>$ echo &#39;fn main() {}&#39; | ltrace -f -e __tls_get_addr rustc - 2&gt;&amp;1 | wc -l
54105
</code></pre></div>



<a name="214103688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103688">(Oct 21 2020 at 19:38)</a>:</h4>
<p>It's not even close to a deterministic number of calls, but it does seem to be much lower.</p>



<a name="214103705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103705">(Oct 21 2020 at 19:39)</a>:</h4>
<p>(And this is the <code>rustc</code> that goes through rustup.)</p>



<a name="214103721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103721">(Oct 21 2020 at 19:39)</a>:</h4>
<p>it's probably much newer :P</p>



<a name="214103833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103833">(Oct 21 2020 at 19:40)</a>:</h4>
<p>locally (previously I was on the server) I get:</p>
<div class="codehilite" data-code-language="Bash Session"><pre><span></span><code><span class="gp">$</span> <span class="nb">echo</span> <span class="s1">''</span> <span class="p">|</span> ltrace -e <span class="s1">'__tls_get_addr-@*LLVM*'</span> -f ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-type<span class="o">=</span>lib --emit<span class="o">=</span>metadata - <span class="m">2</span>&gt;<span class="p">|</span> wc -l
<span class="go">3287</span>
</code></pre></div>



<a name="214103887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103887">(Oct 21 2020 at 19:40)</a>:</h4>
<p>with <code>rustc 1.49.0-nightly (91a79fb29 2020-10-07)</code></p>



<a name="214103897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103897">(Oct 21 2020 at 19:40)</a>:</h4>
<p>Huh!</p>



<a name="214103908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103908">(Oct 21 2020 at 19:40)</a>:</h4>
<p>I get much more than that, with a recent nightly.</p>



<a name="214103927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103927">(Oct 21 2020 at 19:40)</a>:</h4>
<div class="codehilite"><pre><span></span><code>$ echo &#39;fn main() {}&#39; | ltrace -f -e __tls_get_addr ~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc - 2&gt;&amp;1 | wc -l
54391
$ echo &#39;fn main() {}&#39; | ltrace -f -e __tls_get_addr ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc - 2&gt;&amp;1 | wc -l
51085
</code></pre></div>



<a name="214103959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103959">(Oct 21 2020 at 19:41)</a>:</h4>
<p>Oh, you're <em>only</em> tracing LLVM.</p>



<a name="214103963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103963">(Oct 21 2020 at 19:41)</a>:</h4>
<p>you do need the exact arguments I use</p>



<a name="214103986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103986">(Oct 21 2020 at 19:41)</a>:</h4>
<p>there's a <code>-</code> before the <code>@</code>, I'm excluding LLVM</p>



<a name="214103991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103991">(Oct 21 2020 at 19:41)</a>:</h4>
<p>/me goes back to trying to eliminate the calls, instead. :)</p>



<a name="214103999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214103999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214103999">(Oct 21 2020 at 19:41)</a>:</h4>
<p>(Ah.)</p>



<a name="214104030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214104030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214104030">(Oct 21 2020 at 19:41)</a>:</h4>
<p><code>echo '' | ltrace -e '__tls_get_addr-@*LLVM*' -f ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-type=lib --emit=metadata - 2&gt;&amp;1 | wc -l</code></p>



<a name="214104078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214104078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214104078">(Oct 21 2020 at 19:42)</a>:</h4>
<p>if you want to get a comparable number (to my <code>3287</code>)</p>



<a name="214104148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214104148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214104148">(Oct 21 2020 at 19:42)</a>:</h4>
<p>wait, you're not getting the errors anymore?</p>



<a name="214104403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214104403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214104403">(Oct 21 2020 at 19:44)</a>:</h4>
<p>Doesn't seem so. I think it might be timing-dependent.</p>



<a name="214104428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214104428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214104428">(Oct 21 2020 at 19:44)</a>:</h4>
<p>Perhaps the more focused commands aren't taking as long to trace.</p>



<a name="214105429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214105429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214105429">(Oct 21 2020 at 19:52)</a>:</h4>
<p>The ideal solution <em>would</em> be if a top-level compile of a <code>cdylib</code> used <code>local-dynamic</code> with an option for <code>initial-exec</code>, a top-level compile of a dynamic <code>bin</code> used <code>initial-exec</code> for libraries and <code>local-exec</code> for the binary's own things, and a top-level compile of a static <code>bin</code> used <code>local-exec</code> for everything.</p>



<a name="214105501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214105501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214105501">(Oct 21 2020 at 19:52)</a>:</h4>
<p>Rather than having to decide on a crate-by-crate basis.</p>



<a name="214105710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214105710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214105710">(Oct 21 2020 at 19:53)</a>:</h4>
<p>maybe I should just try the thing I was imagining, should be pretty easy</p>



<a name="214105807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214105807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214105807">(Oct 21 2020 at 19:54)</a>:</h4>
<p>Which thing?</p>



<a name="214105856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214105856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214105856">(Oct 21 2020 at 19:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> <a href="#narrow/stream/247081-t-compiler.2Fperformance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs/near/214103039">said</a>:</p>
<blockquote>
<p>but surely if you have a <code>thread_local!</code> in a crate and statically link to it, it won't call <code>__tls_get_addr</code>?</p>
</blockquote>
<p>This?</p>



<a name="214106238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106238">(Oct 21 2020 at 19:57)</a>:</h4>
<p><code>echo 'thread_local!(pub static X: () = {};);' | rustc --crate-type=dylib --crate-name=dylib -Cprefer-dynamic -o libdylib.so - &amp;&amp; echo 'extern crate dylib; fn main() { dylib::X.with(|_| {}); }' | rustc -o test-dylib -L. - &amp;&amp; ltrace -e __tls_get_addr ./test-dylib</code></p>



<a name="214106253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106253">(Oct 21 2020 at 19:57)</a>:</h4>
<p>so this tests for the dylib case, which clearly needs it</p>



<a name="214106357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106357">(Oct 21 2020 at 19:58)</a>:</h4>
<p>True. That case needs global-dynamic, right?</p>



<a name="214106386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106386">(Oct 21 2020 at 19:58)</a>:</h4>
<p><code>echo 'thread_local!(pub static X: () = {};);' | rustc --crate-type=lib --crate-name=rlib -o librlib.rlib - &amp;&amp; echo 'extern crate rlib; fn main() { rlib::X.with(|_| {}); }' | rustc -o test-rlib -L. - &amp;&amp; ltrace -e __tls_get_addr ./test-rlib</code></p>



<a name="214106400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106400">(Oct 21 2020 at 19:58)</a>:</h4>
<p>this outputs <em>nothing</em></p>



<a name="214106439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106439">(Oct 21 2020 at 19:58)</a>:</h4>
<p>time to chain them :P</p>



<a name="214106573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106573">(Oct 21 2020 at 19:59)</a>:</h4>
<p>Currently testing this: <a href="https://github.com/rust-lang/rust/pull/78201">https://github.com/rust-lang/rust/pull/78201</a></p>



<a name="214106694"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106694" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106694">(Oct 21 2020 at 20:00)</a>:</h4>
<p><code>echo 'thread_local!(pub static X: () = {};);' | rustc --crate-type=lib --crate-name=rlib -o librlib.rlib - &amp;&amp; echo 'pub extern crate rlib;' | rustc --crate-type=dylib --crate-name=dylib -Cprefer-dynamic -o libdylib.so -L. -  &amp;&amp; echo 'extern crate dylib; fn main() { dylib::rlib::X.with(|_| {}); }' | rustc -o test-both -L. - &amp;&amp; ltrace -e __tls_get_addr ./test-both</code></p>



<a name="214106730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106730">(Oct 21 2020 at 20:00)</a>:</h4>
<p>this does show <code>__tls_get_addr</code> calls, even if I didn't change how the <code>rlib</code> is compiled</p>



<a name="214106749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106749">(Oct 21 2020 at 20:00)</a>:</h4>
<p>so yeah it's based on how it's used</p>



<a name="214106852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106852">(Oct 21 2020 at 20:01)</a>:</h4>
<p>sadly <code>-Z tls-model=initial-exec</code> on the dylib compilation doesn't change anything</p>



<a name="214106991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214106991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214106991">(Oct 21 2020 at 20:02)</a>:</h4>
<p>fascinating, you need <code>-Z tls-model=initial-exec</code> for the crate that <em>defines</em> the thread-local</p>



<a name="214107033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107033">(Oct 21 2020 at 20:02)</a>:</h4>
<p>even though the static vs dynamic choice doesn't happen until a <code>dylib</code> or executable is linked</p>



<a name="214107068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107068">(Oct 21 2020 at 20:02)</a>:</h4>
<p>so presumably we could have a flag that we only need to pass when compiling <code>rustc_driver</code></p>



<a name="214107109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107109">(Oct 21 2020 at 20:03)</a>:</h4>
<p>I guess this is what you were saying before</p>



<a name="214107160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107160">(Oct 21 2020 at 20:03)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> the PR is a good approximation, maybe we want to check for the path being in <code>compiler/</code>?</p>



<a name="214107260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107260">(Oct 21 2020 at 20:04)</a>:</h4>
<p>there's some crates that start with <code>rustc_</code> which come from <code>crates.io</code> and which may be used elsewhere though I guess thread locals in a <em>dependency</em> of libstd would be highly unusual</p>



<a name="214107598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107598">(Oct 21 2020 at 20:07)</a>:</h4>
<p>guess I should comment on it</p>



<a name="214107946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214107946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214107946">(Oct 21 2020 at 20:09)</a>:</h4>
<p>(responded)</p>



<a name="214108073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214108073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214108073">(Oct 21 2020 at 20:10)</a>:</h4>
<p>definitely agree, overapproximating here is good because we should see the best-case improvements</p>



<a name="214108479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214108479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214108479">(Oct 21 2020 at 20:13)</a>:</h4>
<p>wait I have an idea</p>



<a name="214109426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214109426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214109426">(Oct 21 2020 at 20:20)</a>:</h4>
<p><span class="user-mention" data-user-id="239881">@Josh Triplett</span> <span class="user-mention" data-user-id="116122">@simulacrum</span>  <del>extremely</del> convoluted plan if we want perfect tracking of which crates use TLS <a href="https://github.com/rust-lang/rust/pull/78201#issuecomment-713850554">https://github.com/rust-lang/rust/pull/78201#issuecomment-713850554</a></p>



<a name="214122038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214122038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214122038">(Oct 21 2020 at 22:17)</a>:</h4>
<p>The local-dynamic only has an advantage over global-dynamic when you are accessing two or more dofferent tls variables from the same function. Otherwise it results in an ummecessary extra add and relocation. global-dynamic directly requests the tls var address while local-dynamic requests the address of the tls storage for the current module and then adds the correct offset. This is only faster then multiple __tls_get_addr calls for different tls vars can be merged.</p>



<a name="214146046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214146046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214146046">(Oct 22 2020 at 05:50)</a>:</h4>
<p>Looks like the change is <em>definitely</em> worth making. Now we just need to figure out the best version of the change.</p>



<a name="214146081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214146081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214146081">(Oct 22 2020 at 05:51)</a>:</h4>
<p>Ideally we would apply this to every crate that isn't part of std or proc macros.</p>



<a name="214162470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214162470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214162470">(Oct 22 2020 at 09:27)</a>:</h4>
<p>It can't be applied to hotpluggable codegen backends like cg_clif once it can be built as part of rustc.</p>



<a name="214185039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/__tls_get_addr%20and%20TLS%20in%20dylibs/near/214185039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/__tls_get_addr.20and.20TLS.20in.20dylibs.html#214185039">(Oct 22 2020 at 13:17)</a>:</h4>
<p>Mode::Compiler should work, as I think I suggested on the PR. Happy to help if I didn't explain sufficiently</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>